// lib/models/recommend_user.dart
import 'dart:convert'; // 导入 json 库

// 推荐用户列表的整体响应模型
class RecommendUserListResponse {
  final int code;
  final int count;
  final int current;
  final List<RecommendUser> data;
  final Map<String, dynamic>? extra;
  final String message;
  final int pages;
  final String timestamp;

  RecommendUserListResponse({
    required this.code,
    required this.count,
    required this.current,
    required this.data,
    this.extra,
    required this.message,
    required this.pages,
    required this.timestamp,
  });

  // 从 dynamic JSON 创建 RecommendUserListResponse
  factory RecommendUserListResponse.fromJson(dynamic json) {
    if (json is! Map<String, dynamic>) {
      throw FormatException(
        'Invalid JSON format for RecommendUserListResponse',
      );
    }

    // 解析 data 列表
    List<RecommendUser> userList = [];
    if (json['data'] != null && json['data'] is List) {
      userList =
          (json['data'] as List)
              .map((item) => RecommendUser.fromJson(item))
              .toList();
    }

    return RecommendUserListResponse(
      code: json['code'] as int? ?? -1,
      count: json['count'] as int? ?? 0,
      current: json['current'] as int? ?? 0,
      data: userList,
      extra: json['extra'] as Map<String, dynamic>?,
      message: json['message'] as String? ?? 'Success',
      pages: json['pages'] as int? ?? 0,
      timestamp: json['timestamp'] as String? ?? '',
    );
  }
}

// 单个推荐用户的数据模型
class RecommendUser {
  final int age;
  final String appCode;
  final String appLanguage;
  final int appearanceAuth;
  final int attentionFlag;
  final String birthday;
  final int charmScore;
  final int code; // 注意：这个 code 可能是用户内部的 code，与外层 ApiResponse 的 code 不同
  final bool conversationFlag;
  final String countryCode;
  final String countryCodeOrigin;
  final String countryFlag;
  final String countryGroup;
  final String countryName;
  final String countryRegion;
  final int disturbFlag;
  final int height;
  final String icon;
  final String iconFrame;
  final int id;
  final String idCard;
  final bool isNewUser;
  final int isVip;
  final String job;
  final String latestAlbum;
  final int likeFlag;
  final String makeDate;
  final String name;
  final String onlineDate;
  final String onlineDateStr;
  final int onlineStatus;
  final String onlineStatusStr;
  final int personAuth;
  final int realOnlineStatus;
  final int recommendCharm;
  final String relationship;
  final int sex;
  final int showContactFlag;
  final String signature;
  final int videoFlag;
  final int videoUnitPrice;
  final int videoUnitPriceOriginal;
  final int vipLevel;
  final String vipValidDate;
  final int vistarAuth;
  final int voiceFlag;
  final int voiceUnitPrice;
  final int weight;

  RecommendUser({
    required this.age,
    required this.appCode,
    required this.appLanguage,
    required this.appearanceAuth,
    required this.attentionFlag,
    required this.birthday,
    required this.charmScore,
    required this.code,
    required this.conversationFlag,
    required this.countryCode,
    required this.countryCodeOrigin,
    required this.countryFlag,
    required this.countryGroup,
    required this.countryName,
    required this.countryRegion,
    required this.disturbFlag,
    required this.height,
    required this.icon,
    required this.iconFrame,
    required this.id,
    required this.idCard,
    required this.isNewUser,
    required this.isVip,
    required this.job,
    required this.latestAlbum,
    required this.likeFlag,
    required this.makeDate,
    required this.name,
    required this.onlineDate,
    required this.onlineDateStr,
    required this.onlineStatus,
    required this.onlineStatusStr,
    required this.personAuth,
    required this.realOnlineStatus,
    required this.recommendCharm,
    required this.relationship,
    required this.sex,
    required this.showContactFlag,
    required this.signature,
    required this.videoFlag,
    required this.videoUnitPrice,
    required this.videoUnitPriceOriginal,
    required this.vipLevel,
    required this.vipValidDate,
    required this.vistarAuth,
    required this.voiceFlag,
    required this.voiceUnitPrice,
    required this.weight,
  });

  // 从 dynamic JSON 创建 RecommendUser 对象
  factory RecommendUser.fromJson(dynamic json) {
    if (json is! Map<String, dynamic>) {
      throw FormatException('Invalid JSON format for RecommendUser: $json');
    }
    return RecommendUser(
      age: json['age'] as int? ?? 0,
      appCode: json['appCode'] as String? ?? '',
      appLanguage: json['appLanguage'] as String? ?? '',
      appearanceAuth: json['appearanceAuth'] as int? ?? 0,
      attentionFlag: json['attentionFlag'] as int? ?? 0,
      birthday: json['birthday'] as String? ?? '',
      charmScore: json['charmScore'] as int? ?? 0,
      code: json['code'] as int? ?? 0,
      conversationFlag: json['conversationFlag'] as bool? ?? false,
      countryCode: json['countryCode'] as String? ?? '',
      countryCodeOrigin: json['countryCodeOrigin'] as String? ?? '',
      countryFlag: json['countryFlag'] as String? ?? '',
      countryGroup: json['countryGroup'] as String? ?? '',
      countryName: json['countryName'] as String? ?? '',
      countryRegion: json['countryRegion'] as String? ?? '',
      disturbFlag: json['disturbFlag'] as int? ?? 0,
      height: json['height'] as int? ?? 0,
      icon: json['icon'] as String? ?? '',
      iconFrame: json['iconFrame'] as String? ?? '',
      id: json['id'] as int? ?? 0,
      idCard: json['idCard'] as String? ?? '',
      isNewUser: json['isNewUser'] as bool? ?? false,
      isVip: json['isVip'] as int? ?? 0,
      job: json['job'] as String? ?? '',
      latestAlbum: json['latestAlbum'] as String? ?? '',
      likeFlag: json['likeFlag'] as int? ?? 0,
      makeDate: json['makeDate'] as String? ?? '',
      name: json['name'] as String? ?? '',
      onlineDate: json['onlineDate'] as String? ?? '',
      onlineDateStr: json['onlineDateStr'] as String? ?? '',
      onlineStatus: json['onlineStatus'] as int? ?? 0,
      onlineStatusStr: json['onlineStatusStr'] as String? ?? '',
      personAuth: json['personAuth'] as int? ?? 0,
      realOnlineStatus: json['realOnlineStatus'] as int? ?? 0,
      recommendCharm: json['recommendCharm'] as int? ?? 0,
      relationship: json['relationship'] as String? ?? '',
      sex: json['sex'] as int? ?? 0,
      showContactFlag: json['showContactFlag'] as int? ?? 0,
      signature: json['signature'] as String? ?? '',
      videoFlag: json['videoFlag'] as int? ?? 0,
      videoUnitPrice: json['videoUnitPrice'] as int? ?? 0,
      videoUnitPriceOriginal: json['videoUnitPriceOriginal'] as int? ?? 0,
      vipLevel: json['vipLevel'] as int? ?? 0,
      vipValidDate: json['vipValidDate'] as String? ?? '',
      vistarAuth: json['vistarAuth'] as int? ?? 0,
      voiceFlag: json['voiceFlag'] as int? ?? 0,
      voiceUnitPrice: json['voiceUnitPrice'] as int? ?? 0,
      weight: json['weight'] as int? ?? 0,
    );
  }
}

// Extra 数据模型 (如果需要更细致的解析)
class Extra {
  final int videoUnitPrice;

  Extra({required this.videoUnitPrice});

  factory Extra.fromJson(Map<String, dynamic> json) {
    return Extra(videoUnitPrice: json['videoUnitPrice'] as int? ?? 0);
  }
}
